package com.yenlien.traceyun.control.timeTask;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.bean.RestURL;
import com.yenlien.traceyun.control.dao.MessageDao;
import com.yenlien.traceyun.control.dao.RepaymentDao;
import com.yenlien.traceyun.control.entity.Message;
import com.yenlien.traceyun.control.entity.Repayment;
import com.yenlien.traceyun.control.service.rest.RestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * 定时任务处理逾期信息
 */

@Slf4j
@Component
public class ScheduleTask {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private RepaymentDao repaymentDao;

    @Autowired
    private RestService restService;


    private final DateTimeFormatter fmt = DateTimeFormatter.ofPattern(
            "HH:mm:ss"
    );

    /**
     * 自动编辑发送消息
     */
    //@Scheduled(cron = "0 0 */8 * * ?")        //每8个小时执行一次
    //    @Scheduled(cron = "0/3 * * * * ?")       // 每3s 执行一次
    @Scheduled(cron = "0 0 0 * * ?")            //每天凌晨执行一次
    public void schedule04() {
        List<Repayment> all = repaymentDao.findAll(Sort.by(Sort.Direction.ASC, "createTime"));
        //状态   -1：逾期  0：待还款， 1：待审核  2.还款成功
        for (Repayment pay : all) {
            if (pay.getStatus().equals("0")) {
                Date repaymentTime = pay.getRepaymentTime();
                long oldTime = repaymentTime.getTime();
                long newTime = System.currentTimeMillis();
                int number = new BigDecimal(newTime).compareTo(new BigDecimal(oldTime));
                if (number != -1) {      //newTime < oldTime 返回 -1。
                    //发送消息
                    String companyId = pay.getCompanyId();
                    JSONObject str = restService.getJson(RestURL.USERCENTER + "company/companyIdMsg/" + companyId);
                    Message msg = new Message();
                    msg.setAppName("普惠金融");
                    msg.setCreatetime(new Date());
                    msg.setTitle(pay.getRepaymentNum() + "分期逾期提醒");
                    msg.setContent(pay.getName() + "您好，您的" + pay.getRepaymentNum() + "分期贷款已逾期，请尽快处理");
                    msg.setCompanyName(str.getString("abbreviation"));
                    msg.setPid(String.valueOf(System.currentTimeMillis()));
                    msg.setState(0);
                    msg.setType("0");

                    repaymentDao.updateRepay2(pay.getId(), "-1", new Date());     //逾期的情况
                    Message save = messageDao.save(msg);
                    messageDao.updateAllStatus(save.getPid());
                    log.info("未还款定时任务发送消息--------", LocalDateTime.now().format(fmt));
                }
            }
        }
        log.info("检测过期未还款定时任务发送消息", LocalDateTime.now().format(fmt));
    }
}

